<template>
  <label class="v-checkbox"
    :class="{
      'is-disabled': disabled,
      'is-checked': isChecked,
      'is-focus': focus,
      'is-indeterminate': indeterminate
    }"
  >
    <span class="v-checkbox__input">
      <span class="v-checkbox__inner"></span>
      <input
        v-if="trueLabel || falseLabel"
        class="v-checkbox__original"
        type="checkbox"
        :name="name"
        :disabled="disabled"
        :true-value="trueLabel"
        :false-value="falseLabel"
        v-model="model"
        @change="handleChange"
        @focus="focus = true"
        @blur="focus = false">
      <input
        v-else
        class="v-checkbox__original"
        type="checkbox"
        :disabled="disabled"
        :value="label"
        :name="name"
        v-model="model"
        @change="handleChange"
        @focus="focus = true"
        @blur="focus = false">
    </span>
    <span class="v-checkbox__label" v-if="$slots.default || label">
      <slot></slot>
      <template v-if="!$slots.default">{{label}}</template>
    </span>
  </label>
</template>
<script>
  import Emitter from '@/vuego/mixins/emitter';
  import { isBoolean } from '@/vuego/utils/isX';

  export default {
    name: 'VCheckbox',
    componentName: 'VCheckbox',
    props: {
      value: {},
      label: {},
      indeterminate: Boolean,
      disabled: Boolean,
      checked: Boolean,
      name: String,
      trueLabel: [String, Number],
      falseLabel: [String, Number],
    },
    mixins: [Emitter],
    data() {
      return {
        selfModel: false,
        focus: false,
      };
    },
    computed: {
      model: {
        get() {
          if (this.isGroup) {
            return this.store;
          }
          if (this.value !== undefined) {
            return this.value;
          }
          return this.selfModel;
        },
        set(val) {
          if (this.isGroup) {
            let isLimitExceeded = false;

            if (this.checkboxGroup.min !== undefined
              && val.length < this.checkboxGroup.min) {
              isLimitExceeded = true;
            }
            if (this.checkboxGroup.max !== undefined
              && val.length > this.checkboxGroup.max) {
              isLimitExceeded = true;
            }

            if (isLimitExceeded === false) {
              this.dispatch('VCheckboxGroup', 'input', [val]);
            }
          } else {
            this.$emit('input', val);
            this.selfModel = val;
          }
        },
      },
      isChecked() {
        if (isBoolean(this.model)) {
          return this.model;
        }
        if (Array.isArray(this.model)) {
          return this.model.indexOf(this.label) > -1;
        }
        if (this.model !== null && this.model !== undefined) {
          return this.model === this.trueLabel;
        }
        return false;
      },
      isGroup() {
        let parent = this.$parent;
        while (parent) {
          if (parent.$options.componentName !== 'VCheckboxGroup') {
            parent = parent.$parent;
          } else {
            this.checkboxGroup = parent;
            return true;
          }
        }
        return false;
      },
      store() {
        if (this.checkboxGroup) {
          return this.checkboxGroup.value;
        }
        return this.value;
      },
    },

    methods: {
      addToStore() {
        if (
          Array.isArray(this.model) &&
          this.model.indexOf(this.label) === -1
        ) {
          this.model.push(this.label);
        } else {
          this.model = this.trueLabel || true;
        }
      },
      handleChange(ev) {
        this.$emit('change', ev);
        if (this.isGroup) {
          this.$nextTick(() => {
            this.dispatch('VCheckboxGroup', 'change', [this.checkboxGroup.value]);
          });
        }
      },
    },
    created() {
      if (this.checked) {
        this.addToStore();
      }
    },
  };
</script>
